﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace StudentService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "StudentSrv" in code, svc and config file together.
    public class StudentSrv : IStudentSrv
    {
        questEntities db = new questEntities();
        Teacher teacherObj;
        Avatar avatarObj;
        Student studentObj;

        public void saveUser(User userObj)
        {
            db.Users.AddObject(userObj);
            db.SaveChanges();
        }

        public Teacher getTeacherByName(string teacherName)
        {
            teacherObj = new Teacher();
            teacherObj = db.Teachers.SingleOrDefault(p => p.User.first_name == teacherName);
            
            return teacherObj;
        }
        public Teacher saveTeacher(Teacher teacherObj)
        {
            db.Teachers.AddObject(teacherObj);
            db.SaveChanges();
            return teacherObj;
        }
      public Avatar getAvatarByID(int _ID)
        {
            avatarObj = new Avatar();
            avatarObj = db.Avatars.SingleOrDefault(p => p.pk_avatar_id == _ID);

            return avatarObj;
        }

      public Student getStudentByID(int userID)
      {
          studentObj = new Student();
          studentObj = db.Students.SingleOrDefault(p => p.fk_user_id == userID);

          return studentObj;
      }

      public List<Avatar> getAllAvatars()
      {
          return db.Avatars.ToList();
      }

       public bool updateAvatarByID(int student_id, int avatar_id)
        {
            //Avatar av = db.Avatars.First(p => p.pk_avatar_id == avatar_id);
            Student st = db.Students.First(s => s.fk_user_id == student_id);
            st.fk_av_id = avatar_id;
            db.SaveChanges();
            return true;
        }
       public bool addAvatar(Avatar avatarObj)
       {
           db.Avatars.AddObject(avatarObj);
           db.SaveChanges();
           return true;
       }

       public byte[] getStudentAvatar(int student_id)
       {
           System.Linq.IQueryable<byte[]> query;
           query = db.Students
                     .Where(p => p.fk_user_id == student_id).Select(p => p.Avatar.picture);
           return query.ToList<byte[]>().First();
       }


       public String addCurrentStudent(int student_id, byte[] UDID)
       {
           try
           {
               using (var db = new questEntities())
               {
                   Current_Users cuObj = new Current_Users();
                   cuObj.device_unique_id = UDID;
                   cuObj.login_time = DateTime.Now;
                   cuObj.fk_st_id = student_id;
                   db.Current_Users.AddObject(cuObj);
                   db.SaveChanges();

               }
           }
           catch (Exception ex)
           {
               return ex.InnerException.ToString();
           }
           return "add successful";
       }



       public string removeCurrentStudent(int student_id)
       {
           try
           {
               using (var db = new questEntities())
               {
                   Current_Users cuObj = db.Current_Users.First(p => p.fk_st_id == student_id);

                   db.Current_Users.DeleteObject(cuObj);
                   db.SaveChanges();
               }
           }
           catch (Exception ex)
           {
               return ex.InnerException.ToString();
           }
           return "delete successful";
       }
    
    }
    
}
